import re

import requests
from pip._internal import req
from requests.auth import HTTPBasicAuth

# url = "http://httpbin.org/get"
#
# params = {
#     "name": "test",
#     "age": 18
# }
# response = requests.get(url,params=params)
# print(response.text)
# print(response.url)
# print(response.status_code)
# print(response.json())


# url = "http://httpbin.org/post"
# data = {
#     "name": "test",
#     "age": 18
# }
# response = requests.post(url,data=data)
#
# print(response.text,type(response.text))
# print(response.json())
# print(response.status_code)

#
# url = "http://httpbin.org/post"
# json_data = {
#     "name": "test",
#     "age": 18
# }
#
# response = requests.post(url,json=json_data)
# print(response.json())
# print(response.status_code)


# url = "https://httpbin.org/get"
# response = requests.get(url)
#
# print("content 类型：", type(response.content))
# print("text 类型：", type(response.text))
# json_data = response.json()
# print("json() 类型：", type(json_data))


#
# url = "http://127.0.0.1:8000/admin/"
# req = requests.get(url,allow_redirects=False)
#
# print(req.status_code)
# print(req.headers.get("location"))
#
# location = req.headers.get("location")
# location = f"http://127.0.0.1:8000/admin/{location}"
# req = requests.get(location)
# print(req.status_code)
# print(req.text)



# url = "http://127.0.0.1:8000/admin/"
# headers = {
# #     'cookie': "csrftoken=77Qn1oXlJM8PV8bovwDy58F7NU4ECdm5; sessionid=0w7rg6zwzv9d29d9axi301ufcrz1lsh6"
# }
#
# cookies= {
#     'sessionid':  '0w7rg6zwzv9d29d9axi301ufcrz1lsh6'
# }
# response = requests.request("GET", url, headers=headers,cookies=cookies)
# print(response.text)
# print(response.status_code)

# url = "http://127.0.0.1:8000/admin/"
# # auth_res = requests.get(url,auth=HTTPBasicAuth("admin","123456"))
# # print(auth_res)
# # print(auth_res.text)

#会话
# login_url = "http://127.0.0.1:8000/admin/login/?next=/admin/"
#
# session = requests.Session()
# response = session.get(login_url)
# csrftoken = re.search(r'<input type="hidden" name="csrfmiddlewaretoken" value="(.*)">', response.text).group(1)
# response = session.post(login_url,data={
#     "username": "admin",
#     "password": "123456",
#     "csrfmiddlewaretoken": csrftoken,
# })
# print(response.text)